package com.bgba.oer;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import com.flashline.registry.openapi.base.OpenAPIException;
import com.flashline.registry.openapi.entity.Asset;
import com.flashline.registry.openapi.entity.AssetSummary;
import com.flashline.registry.openapi.entity.AuthToken;
import com.flashline.registry.openapi.entity.Contact;
import com.flashline.registry.openapi.entity.RelationshipType;
import com.flashline.registry.openapi.query.AssetCriteria;
import com.flashline.registry.openapi.query.SearchTerm;
import com.flashline.registry.openapi.service.v300.FlashlineRegistry;
import com.flashline.registry.openapi.service.v300.FlashlineRegistryServiceLocator;

public class SearchAssetTest {


	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("======================== ");
		String urlConnectionLogin = "http://desasoa01/oer/services/FlashlineRegistry?WSDL";
		String username = " L0269239";
		String password = "finde2011";
		/*
		String urlConnectionLogin = "http://172.17.1.10:8101/oer/services/FlashlineRegistry?WSDL";
		String username = " admin";
		String password = "admin123";
		*/
		
		SearchAssetTest instance = new SearchAssetTest();
		instance.searchAsset(urlConnectionLogin, username, password);

		//addRelationships(urlConnectionLogin, username, password);
	}

	private  void searchAsset(String urlConnectionLogin, String username, String password) {
		try {
			///////////////////////////////////////////////////////////
			// Connect to Oracle Enterprise Repository
			///////////////////////////////////////////////////////////
			URL lURL = null;
			lURL = new URL(urlConnectionLogin);
			FlashlineRegistry repository = new FlashlineRegistryServiceLocator().getFlashlineRegistry(lURL);

			///////////////////////////////////
			// Login to OER
			///////////////////////////////////
			AuthToken authToken = repository.authTokenCreate(username, password);

			//////////////////////////////////////
			// asset name for operation
			//////////////////////////////////////
			//String assetNameCriteria = "Operacion - Test";
			//String assetNameCriteria = "Service Gal Test";	
			String assetNameCriteria = "SystemTest 002";	
			
			//String assetNameCriteria = "operacion programatica";
			//String assetNameCriteria = "Operation - Test SAP";			

			Asset myOperationAsset = searchAssetByName(repository, authToken, assetNameCriteria);
			

			System.out.println("========================= myOperationAsset.getID="+myOperationAsset.getID());
			System.out.println("========================= myOperationAsset.getDisplayName="+myOperationAsset.getDisplayName());
			System.out.println("========================= myOperationAsset.getTypeName="+myOperationAsset.getTypeName());
			System.out.println("========================= myOperationAsset.getCustomData="+myOperationAsset.getCustomData());
			System.out.println("========================= myOperationAsset.getDescription="+myOperationAsset.getDescription());

			myOperationAsset.setDescription("la descripcion");
			myOperationAsset = repository.assetUpdate(authToken, myOperationAsset);
			
			System.out.println("========================= myOperationAsset.getDescription="+myOperationAsset.getDescription());
			System.out.println("========================= myOperationAsset.getID="+myOperationAsset.getID());
			System.out.println("========================= myOperationAsset.getDisplayName="+myOperationAsset.getDisplayName());
			System.out.println("========================= myOperationAsset.getTypeName="+myOperationAsset.getTypeName());
			
			String dummyUrl = repository.assetReadCustomDataString(authToken, myOperationAsset.getID(), "dummy-url");
			System.out.println("========================= myOperationAsset.dummyUrl="+dummyUrl);
			
			for (Contact myContact : myOperationAsset.getContacts()) {
				System.out.println("========================= myOperationAsset.contact.getID="+myContact.getID());
				System.out.println("========================= myOperationAsset.contact.getName="+myContact.getName());
			}

			RelationshipType[] allRelationshipTypes = myOperationAsset.getRelationshipTypes();
			System.out.println("========================= allRelationshipTypes.length=="+allRelationshipTypes.length);

			RelationshipType myRelationshipType = null;
			String relationshipName ="Provides"; 
			for (RelationshipType relationshipType : allRelationshipTypes) {
				//System.out.println("=========================== relationshipType.getName="+relationshipType.getName());
				if (relationshipType.getName().equals(relationshipName)){
					//This is the relationship type, modify the assets that are related
					// using it
					myRelationshipType = relationshipType;
					break;
				}
			}
			
			if (myRelationshipType == null ){
				System.out.println("======================================= NOT FOUND Relationship TYPE for assets with name="+assetNameCriteria);				
			}
			
			if (myRelationshipType.getName().equals(relationshipName)){
				if ((myRelationshipType.getPrimaryIDs().length > 0) || (myRelationshipType.getSecondaryIDs().length > 0)){
					System.out.println("================================= myRelationshipType.getDisplayName()="+myRelationshipType.getDisplayName());
					System.out.println("================================= myRelationshipType.getDisplaySecondary()="+myRelationshipType.getDisplaySecondary());

					if (myRelationshipType.getPrimaryIDs().length > 0){
						for (Long relationshipPrimaryId : myRelationshipType.getPrimaryIDs()) {
							System.out.println("====================================== relationshipPrimaryId="+relationshipPrimaryId);
						}
					} else {
						System.out.println("======================================= NOT FOUND Relationship PRIMARY for assets with name="+assetNameCriteria);				
					}

					if (myRelationshipType.getSecondaryIDs().length > 0){
						for (Long relationshipSecondaryId : myRelationshipType.getSecondaryIDs()) {
							System.out.println("====================================== relationshipSecondaryId="+relationshipSecondaryId);
						}
					} else {
						System.out.println("======================================= NOT FOUND Relationship SECONDARY for assets with name="+assetNameCriteria);				
					}
				}
			}
			
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	private Asset searchAssetByName(FlashlineRegistry repository, AuthToken authToken, String assetNameCriteria) throws RemoteException, OpenAPIException {
		System.out.println("\n ######################################################");
		///////////////////////////////////////////////////////////
		// Search assets
		// try a general search which includes Name, version,
		// description, and keywords
		////////////////////////////////////////////////////////////

		AssetCriteria criteria = new AssetCriteria();
		criteria = new AssetCriteria();
		criteria.setGeneralCriteria(assetNameCriteria);

		AssetSummary[] assets = repository.assetQuerySummary(authToken, criteria);
		//criteria.
		System.out.println("======================================= assets="+assets);
		System.out.println("======================================= assets.length="+assets.length);
		Asset myAsset = null;
		if (assets.length == 1){
			AssetSummary assetSummary = assets[0];
			//System.out.println("========================= assetSummary.getID="+assetSummary.getID());
			//System.out.println("========================= assetSummary.getName="+assetSummary.getName());
			//System.out.println("========================= assetSummary.getVersion="+assetSummary.getVersion());
			//System.out.println("========================= assetSummary.getTypeID="+assetSummary.getTypeID());

			myAsset = repository.assetRead(authToken, assetSummary.getID());

			//System.out.println("========================= myAsset.getDisplayName="+myAsset.getDisplayName());
			//System.out.println("========================= myAsset.getTypeName()="+myAsset.getTypeName());
			//System.out.println("========================= myAsset.getTypeName()="+myAsset.getTypeName());

		} else {
			System.out.println("======================================= NOT FOUND assets with name="+assetNameCriteria + ", assets resultSet="+assets.length);				
		}

		return myAsset;
	}

}
